//
//  LTY_ClassViewController.m
//  MooMusic
//
//  Created by 守护神 on 15/10/22.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "LTY_ClassViewController.h"
// 导入解析头文件
#import "NetWork.h"
// 导入model的头文件
#import "LTY_ClassModel.h"
// 导入classtableView的自定义cell的头文件
#import "LTY_ClassTableViewCell.h"
// 导入subTypeModel的头文件
#import "LTYSubtypeModel.h"

@interface LTY_ClassViewController ()<UITableViewDelegate, UITableViewDataSource>
// 创建数据源
@property (nonatomic, strong)NSMutableArray *classArray;
// 创建一个用来存放subType数组的数据源
@property (nonatomic, strong)NSMutableArray *SubtypeArr;
// 创建三个数据源分别传递subTypeArr;
@property (nonatomic, strong)NSMutableArray *typeArray1;

@end

static NSString *cellIndertifier = @"cellIndertifier";

@implementation LTY_ClassViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.classArray = [NSMutableArray array];
        self.SubtypeArr = [NSMutableArray array];
        self.typeArray1 = [NSMutableArray array];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建视图
    [self creatClassTableview];
    // 获取数据
    [self getClassData];
    
    // Do any additional setup after loading the view.
}
// 获取数据
- (void)getClassData
{
    
    [NetWork networkGETRequestWithURL:classString Paramater:nil ResultBlock:^(id result) {
        NSLog(@"+_+_+_  %@", result);
        NSMutableArray *dataArr = [NSMutableArray array];
        dataArr = [result objectForKey:@"data"];
        
        // 调用基类的方法
        self.classArray = [LTY_ClassModel arrayWithModelByArray:dataArr];
        
        NSMutableArray *subArr = [NSMutableArray array];
        for (NSDictionary *dic in dataArr) {
            subArr = [dic valueForKey:@"subType"];
            [self.SubtypeArr addObject:subArr];
        }
        
        
        if (self.classArray.count > 0) {
            // 刷新tableview
            [classTableView reloadData];
        }
        
        
    }];
    
}

// 创建视图
- (void)creatClassTableview
{
    classTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    classTableView.backgroundColor = [UIColor whiteColor];
    classTableView.delegate = self;
    classTableView.dataSource = self;
    // 注册
    [classTableView registerClass:[LTY_ClassTableViewCell class] forCellReuseIdentifier:cellIndertifier];
    classTableView.bounces = NO;
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50)];
    headView.backgroundColor = [UIColor lightGrayColor];
    classTableView.tableHeaderView = headView;
    
    UIButton *headButton = [UIButton buttonWithType:UIButtonTypeCustom];
    headButton.frame = CGRectMake(SCREENWIDTH / 2 - 20, 10, 40, 30);
    [headButton addTarget:self action:@selector(headAction:) forControlEvents:UIControlEventTouchUpInside];
    headButton.tag = 300;
    [headButton setTitle:@"全部" forState:UIControlStateNormal];
    [headView addSubview:headButton];
    
    [self.view addSubview:classTableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

// 分区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.classArray.count > 0) {
            return [[self.classArray objectAtIndex:0] valueForKey:@"name"];
        }else{
            return @"";
        }
    }else if (section == 1){
        if (self.classArray.count > 0) {
            return [[self.classArray objectAtIndex:1] valueForKey:@"name"];
        }else{
            return @"";
        }
    }else{
        if (self.classArray.count > 0) {
            return [[self.classArray objectAtIndex:2] valueForKey:@"name"];
        }else{
            return @"";
        }
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTY_ClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndertifier];
    
    // 取消点击效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 把subType的数据源传递给自定义cell的数组
    if (self.classArray.count > 0) {
        cell.subArray = self.SubtypeArr;
        // 分别把对应的数组传递过去
        cell.TypeArr1 = self.SubtypeArr[indexPath.section];
    }
    
    // pop回到上一个界面
    cell.LBsendBlock = ^(NSString *string, NSNumber *order, NSNumber *contID){
        
        // 协议传值 第三步:  代理人执行方法
        // 代理人存在 代理人响应了你协议的方法
        // 做了一个判断 可选择执行的方法
        if (self.delegate && [self.delegate respondsToSelector:@selector(sendValue:sendOrder:sendId:)]) {
            [self.delegate sendValue:string sendOrder:order sendId:contID];
        }

        
        
        [self.navigationController popViewControllerAnimated:YES];
    };
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

#pragma mark -- 点击headbutton触发的方法
- (void)headAction:(UIButton *)Sender
{
    // 3.触发方法 调用Block
    self.sendBlock(Sender.currentTitle, 1, 1);
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
